/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sims.gui.salesperson;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import sims.entity.Order;
import sims.entity.OrderDetail;
import sims.entity.Product;
import sims.manager_SalesPerson.Function;

/**
 *
 * @author vanthanh
 */
public class OrderCancel extends javax.swing.JPanel {

    /**
     * Creates new form OrderCancel
     */
    ArrayList<String> arrUpdateOrder = new ArrayList<String>();
    ArrayList<String> arrUpdateProduct = new ArrayList<String>();
    DefaultTableModel modelview = new DefaultTableModel() {
        @Override
        public boolean isCellEditable(int i, int i1) {
            return false;
        }
    };

    public OrderCancel() {
        initComponents();
    }
        String userName;
        String orderId;
    public OrderCancel(String orderId, String userName) {
        this.userName = userName;
        this.orderId = orderId;
        initComponents();
        String UpdateOrder = "update orders set status = 'cancelled' where orderId ='" + orderId + "'";
        arrUpdateOrder.add(UpdateOrder);
        Function fun = new Function();
        Order order = new Order();
        ArrayList<OrderDetail> arrOrderDetail = new ArrayList<OrderDetail>();
        String[] column = {"STT", "ItemID", "Item Name", "Unit", "Price", "Amount", "Cost"};
        modelview.setColumnIdentifiers(column);

        /* cua order view */

        order = fun.getOrderbyId(orderId);
        arrOrderDetail = fun.getOrderDetailByOrderId(orderId);
        lbDatetime.setText(order.getDatetime().toString());
        
        lbUserName.setText(fun.getUserName(order.getUserId()));
        lbTotal.setText(String.valueOf(order.getTotal()));
        int stt = 0;
        for (OrderDetail od : arrOrderDetail) {
            String updateProduct = "update item set quantityAvailable += " + od.getAmount() + " where itemId='" + od.getItemID() + "'";
            arrUpdateProduct.add(updateProduct);
            stt++;
            Product pro = new Product();
            pro = fun.getProductById(od.getItemID());
            Object[] row = {stt, od.getItemID(), pro.getProductName(), pro.getUnit(), od.getPriceSell(), od.getAmount(), (od.getPriceSell() * od.getAmount())};
            modelview.addRow(row);
        }
        jTable_OrderSell.setModel(modelview);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnOrderCancel = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable_OrderSell = new javax.swing.JTable();
        lbDatetime = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        lbTotal = new javax.swing.JLabel();
        lbUserName = new javax.swing.JLabel();
        bnSubmit = new javax.swing.JButton();
        bnCencel = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        pnOrderCancel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("Cancel Order ");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setText("Datetime :");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel3.setText("User Name:");

        jTable_OrderSell.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTable_OrderSell);

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel8.setText("Total:");

        bnSubmit.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        bnSubmit.setText("Submit");
        bnSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnSubmitActionPerformed(evt);
            }
        });

        bnCencel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        bnCencel.setText("Cancel");
        bnCencel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnCencelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(lbTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 537, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(32, 32, 32)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(lbDatetime, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
                                    .addComponent(lbUserName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(149, 149, 149)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(166, 166, 166)
                .addComponent(bnSubmit)
                .addGap(36, 36, 36)
                .addComponent(bnCencel)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(lbDatetime, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(lbUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8)
                    .addComponent(lbTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bnCencel)
                    .addComponent(bnSubmit))
                .addGap(19, 19, 19))
        );

        javax.swing.GroupLayout pnOrderCancelLayout = new javax.swing.GroupLayout(pnOrderCancel);
        pnOrderCancel.setLayout(pnOrderCancelLayout);
        pnOrderCancelLayout.setHorizontalGroup(
            pnOrderCancelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        pnOrderCancelLayout.setVerticalGroup(
            pnOrderCancelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        add(pnOrderCancel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void bnSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnSubmitActionPerformed
        int Submit = JOptionPane.showConfirmDialog(this, "are you sure want cancel ?", null, JOptionPane.YES_NO_OPTION);
        if (Submit == JOptionPane.OK_OPTION) {
            Function fun = new Function();
            fun.runBath(arrUpdateProduct);
            fun.runBath(arrUpdateOrder);
            JOptionPane.showMessageDialog(this, "Order Cancelled  successfull");
            this.removeAll();
            ListSellOrder listOrder = new ListSellOrder(userName);
            this.add(listOrder);
            this.validate();
        }
    }//GEN-LAST:event_bnSubmitActionPerformed

    private void bnCencelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnCencelActionPerformed
            this.removeAll();
            ListSellOrder listOrder = new ListSellOrder(userName);
            this.add(listOrder);
            this.validate();
    }//GEN-LAST:event_bnCencelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bnCencel;
    private javax.swing.JButton bnSubmit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable_OrderSell;
    private javax.swing.JLabel lbDatetime;
    private javax.swing.JLabel lbTotal;
    private javax.swing.JLabel lbUserName;
    private javax.swing.JPanel pnOrderCancel;
    // End of variables declaration//GEN-END:variables
}
